/*  i2c address defintion file  */
#ifndef I2C_H
#define I2C_H

#define HW_I2_BASE						(0x02000080)

#define HW_I2C_RESET       				(HW_I2_BASE+0x000)
#define HW_I2C_TIMING      				(HW_I2_BASE+0x004)
#define HW_I2C_SLAVE      				(HW_I2_BASE+0x008)
#define HW_I2C_TRANSFER_CONFIG			(HW_I2_BASE+0x00C)
#define HW_I2C_DATA						(HW_I2_BASE+0x010)
#define HW_I2C_TRANSFER_START			(HW_I2_BASE+0x014)
#define HW_I2C_INT_STATUS				(HW_I2_BASE+0x020)
#define HW_I2C_INT_MASK					(HW_I2_BASE+0x024)
#define HW_I2C_INT_CLEAR				(HW_I2_BASE+0x020)

//bits for HW_I2C_TIMING register
/* T1, HIGH period of SCL clockSTART hold timeSTOP setup time*/
#define I2C_TIMING_T1					(0x0E)
/* T2, LOW period of SCL clockBus free period */
#define I2C_TIMING_T2					(0x112<<11)
/* T3, DATA hold time */
#define I2C_TIMING_T3					(0x124<<22)

//bits for HW_I2C_SLAVE register
#define I2C_SLAVE_ADDRESS				(0x00000034)
#define I2C_SLAVE_ACK					(0x00000001)
#define I2C_SLAVE_NACK					(0x00000000)

//bits for HW_I2C_TRANSFER_CONFIG register
#define I2C_TRANSFER_CONFIG_BYTE_NUM1	0x00000010
#define I2C_TRANSFER_CONFIG_BYTE_NUM2	0x00000020
#define I2C_TRANSFER_CONFIG_BYTE_NUM3	0x00000030
#define I2C_TRANSFER_CONFIG_BYTE_NUM4	0x00000040
#define I2C_TRANSFER_CONFIG_WRITE		0x00000000
#define I2C_TRANSFER_CONFIG_READ		0x00000001
#define I2C_TRANSFER_CONFIG_NORMAL		0x00000000
#define I2C_TRANSFER_CONFIG_COMBIN		0x00000002

//bits for HW_I2C_TRANSFER_START register
#define I2C_TRANSFER_START_ENABLE		0x00000001
#define I2C_TRANSFER_START_DISABLE		0x00000000

//bits for HW_I2C_INT_STATUS register
#define I2C_INT_STATUS_ACK_ERR			0x00000004
#define I2C_INT_STATUS_WR_DONE			0x00000002
#define I2C_INT_STATUS_RD_DONE			0x00000001

//bits for HW_I2C_INT_MASK register
#define I2C_INT_MASK_ACK_ERR			0x00000004
#define I2C_INT_MASK_WR_DONE			0x00000002
#define I2C_INT_MASK_RD_DONE			0x00000001

//bits for HW_I2C_INT_CLEAR register
#define I2C_INT_CLEAR_ACK_ERR			0x00000004
#define I2C_INT_CLEAR_WR_DONE			0x00000002
#define I2C_INT_CLEAR_RD_DONE			0x00000001

#define I2C_READ_DATA_LEN_MAX           4
#define I2C_WRITE_DATA_LEN_MAX          4

#define	I2C_NORMAL                     (0x00)
#define	I2C_COMBIN                     (0x01)
#define	I2C_NACK                       (0x00)
#define	I2C_ACK							(0x02)

#define I2C_TIMEOUT_VALUE				(0x1000)

#ifndef UINT8
typedef unsigned char 		UINT8;
#endif
#ifndef UCHAR
typedef unsigned char 		UCHAR;
#endif
#ifndef UINT16
typedef unsigned short int	UINT16;
#endif
#ifndef UINT32
typedef unsigned long int	UINT32;
#endif

/* read/write command type for i2c slave I/O operation */
typedef  struct {
	UINT8	   slave_addr;				/* slave address */
	UINT8	   options;                     /* bit mask of slave control actions */  
	UINT8    reg_len;                             /* address of register in slave */
	UINT8    len;                             /* count of bytes to transfer,max 4 each transfer */
	UINT8   *buf_ptr;                   /* pointer to buffer in caller space */
} I2C_RW_CMD_TYPE_T;

typedef struct {
	UINT32	timming;
	UINT8	retry_time;
	UINT16 	access_interval;
	UINT8	slave_addr;
	UINT8	options;
	UINT8	len_per_write;
	UINT8	len_per_read;
}I2C_PARA_T;

typedef struct {
	I2C_PARA_T *p_para_i2c;
	UINT8   *data; 	//for write
	UINT32	data_size;
	UINT8	*result;	//for read back
}I2C_REQ_T;

/*===========================================================================
                      FUNCTION DECLARATIONS
===========================================================================*/
#if 0
extern void i2c_init(I2C_PARA_T *p_para_i2c);
#endif
extern STATUS_T i2c_write(I2C_PARA_T *p_para_i2c,UINT8 *buf);
extern STATUS_T i2c_read(I2C_PARA_T *p_para_i2c,UINT8 *buf,UINT8* read_buf);

/*API*/
extern STATUS_T i2c_write_buffer(I2C_REQ_T * i2c_req);
extern STATUS_T i2c_read_buffer(I2C_REQ_T *i2c_req);



#if 1
	#define MA_OUTW( io, val )  	outpw( (io), (int) (val))
	#define MA_OUTW2( io, val ) 	outpw( (io), (int) (val))
	#define MA_OUTB( io, val ) 	outpb( (io), (int) (val))
	#define MA_INW( io )  			(UINT32) inpw( io )
	#define MA_INW2( io )  		(UINT32) inpw( io )
	#define MA_INB( io )  			(UINT8) inpb( io )
	#define MA_INWM( io, mask ) 	( MA_INW(io) & (mask) )

	#define delay(x)
#endif

#endif  /*I2C_H*/
